package test06;

import java.util.*;

public class Test06 {
    //Todo 依然没搞懂
    public static void main(String[] args) {
        // 创建 TreeSet，按平均分从高到低排序（降序）
        TreeSet<Student> students = new TreeSet<>((a, b) ->
                Double.compare(b.getAverage(), a.getAverage())  // 降序：高分在前
        );

        // 添加学生（自动排序）
        students.add(new Student("001", "张三",85, 90, 88));
        students.add(new Student("002", "李四", 78, 82, 80));
        students.add(new Student("003", "王五", 92, 95, 90));
        students.add(new Student("004", "赵六",88, 86, 90));

        // 打印排序结果
        System.out.println(" 排序后结果（按平均分从高到低）：");
        for (Student s : students) {
            System.out.println(s);
        }

        // 插入一个新学生
        Student newStu = new Student("005", "孙七", 95, 92, 94);
        students.add(newStu);
        System.out.println("\n➕ 插入孙七后：");
        for (Student s : students) {
            System.out.println(s);
        }

        // 删除一个学生（需要先找到对象，TreeSet 不支持直接按 id 删除）
        Student toRemove = null;
        for (Student s : students) {
            if (s.getId().equals("002")) {
                toRemove = s;
                break;
            }
        }
        if (toRemove != null) {
            students.remove(toRemove);
            System.out.println("\n➖ 删除李四后：");
            for (Student s : students) {
                System.out.println(s);
            }
        }
    }
}
